#!/usr/bin/python3
# coding: utf-8

class QuestionPaser:
    '''构建实体节点'''

    def build_entitydict(self, args):
        entity_dict = {}
        for arg, types in args.items():
            for type in types:
                if type not in entity_dict:
                    entity_dict[type] = [arg]
                else:
                    entity_dict[type].append(arg)

        return entity_dict

    '''解析主函数'''

    def parser_main(self, res_classify):
        args = res_classify['args']
        entity_dict = self.build_entitydict(args)
        question_types = res_classify['question_types']
        sqls = []
        for question_type in question_types:
            sql_ = {}
            sql_['question_type'] = question_type
            sql = []
            if question_type == 'error_err_cause':
                sql = self.sql_transfer(question_type, entity_dict.get('error'))

            elif question_type == 'err_cause_error':
                sql = self.sql_transfer(question_type, entity_dict.get('err_cause'))

            elif question_type == 'error_err_cause':
                sql = self.sql_transfer(question_type, entity_dict.get('error'))

            elif question_type == 'error_err_effect':
                sql = self.sql_transfer(question_type, entity_dict.get('error'))
            elif question_type == 'err_effect_error':
                sql = self.sql_transfer(question_type, entity_dict.get('err_effect'))


            elif question_type == 'error_solve_way':
                sql = self.sql_transfer(question_type, entity_dict.get('error'))

            elif question_type == 'solve_way_error':
                sql = self.sql_transfer(question_type, entity_dict.get('solve_way'))

            elif question_type == 'error_check_way':
                sql = self.sql_transfer(question_type, entity_dict.get('error'))

            elif question_type == 'check_way_error':
                sql = self.sql_transfer(question_type, entity_dict.get('check_way'))

            elif question_type == 'error_server':
                sql = self.sql_transfer(question_type, entity_dict.get('error'))
            elif question_type == 'server_error':
                sql = self.sql_transfer(question_type, entity_dict.get('server'))

            elif question_type == 'link_module_error':
                sql = self.sql_transfer(question_type, entity_dict.get('link_module'))
            elif question_type == 'error_link_module':
                sql = self.sql_transfer(question_type, entity_dict.get('error'))
            elif question_type == 'error_desc':
                sql = self.sql_transfer(question_type, entity_dict.get('error'))

            if sql:
                sql_['sql'] = sql

                sqls.append(sql_)

        return sqls

    '''针对不同的问题，分开进行处理'''

    def sql_transfer(self, question_type, entities):
        if not entities:
            return []

        # 查询语句
        sql = []
        # 查询疾病的持续时间
        if question_type == 'error_server':
            sql = [
                "MATCH (m:error)-[r:runing_on]->(n:server) where m.name = '{0}' return m.name, r.name, n.name".format(i)
                for i in entities]
        # 查询故障相应模块
        if question_type == 'error_link_module':
            sql = [
                "MATCH (m:error)-[r:maybe_module]->(n:link_module) where m.name = '{0}' return m.name, r.name, n.name".format(
                    i) for i in entities]
        # 查询疾病的相关介绍
        elif question_type == 'error_desc':
            sql = ["MATCH (m:error) where m.name = '{0}' return m.name, m.desc".format(i) for i in entities]

        # 查询疾病有哪些症状
        elif question_type == 'error_err_cause':
            sql = [
                "MATCH (m:error)-[r:has_err_cause]->(n:err_cause) where m.name = '{0}' return m.name, r.name, n.name".format(
                    i) for i in entities]

        # 查询症状会导致哪些疾病
        elif question_type == 'err_cause_error':
            sql = [
                "MATCH (m:error)-[r:has_err_cause]->(n:err_cause) where n.name = '{0}' return m.name, r.name, n.name".format(
                    i) for i in entities]

        # 查询疾病的并发症
        elif question_type == 'error_err_effect':
            sql = [
                "MATCH (m:error)-[r:produce_effect]->(n:err_effect) where m.name = '{0}' return m.name, r.name, n.name".format(
                    i) for i in entities]


        # 查询疾病常用药品－药品别名记得扩充
        elif question_type == 'error_solve_way':
            sql = [
                "MATCH (m:error)-[r:rels_solve_way]->(n:solve_way) where m.name = '{0}' return m.name, r.name, n.name".format(
                    i) for i in entities]

        # 已知药品查询能够治疗的疾病
        elif question_type == 'solve_way_error':
            sql = [
                "MATCH (m:error)-[r:rels_solve_way]->(n:solve_way) where n.name = '{0}' return m.name, r.name, n.name".format(
                    i) for i in entities]
        # 查询疾病应该进行的检查
        elif question_type == 'error_check_way':
            sql = [
                "MATCH (m:error)-[r:need_check_way]->(n:check_way) where m.name = '{0}' return m.name, r.name, n.name".format(
                    i) for i in entities]

        # 已知检查查询疾病
        elif question_type == 'check_way_error':
            sql = [
                "MATCH (m:error)-[r:need_check_way]->(n:check_way) where n.name = '{0}' return m.name, r.name, n.name".format(
                    i) for i in entities]

        return sql


if __name__ == '__main__':
    handler = QuestionPaser()
